Operator Precedence in Java

Note: Use explicit parentheses when there is even the possibility of confusion.

The operators in Java, shown in order of precedence - from highest to lowest
Priority Operators Operation Associativity
1 [ ] array index left
() method call
. member access
2 ++ pre- or postfix increment right
-- pre- or postfix decrement
+ - unary plus, minus
~ bitwise NOT
! boolean (logical) NOT
(type) type cast
new object creation
3 * / % multiplication, division, remainder left
4 + - addition, substraction left
+ string concatenation
5 << signed bit shift left left
>> signed bit shift right
>>> unsigned bit shift right
6 < <= less than, less than or equal to left
> >= greater than, greater than or equal to
instanceof reference test
7 == equal to left
!= not equal to
8 & bitwise AND left
& boolean (logical) AND
9 ^ bitwise XOR left
^ boolean (logical) XOR
10 | bitwise OR left
| boolean (logical) OR
11 && boolean (logical) AND left
12 || boolean (logical) OR left
13 ? : conditional right
14 = assignment right
*= /= += -= %=
<<= >>= >>>=
&= ^= |=
combinated assignment
(operation and assignment)

Associativity

Operators with the same precedence level in an expression are evaluated based on their associativity.
For example, left-associative operators group from left-to-right. Therefore, the expression

x * y % z
is equivalent to
(x * y) % z

Similarly
x = y = 5
is evaluated right-to-left :
x = (y = 5)

Summary

These are the general categories of operators evaluated from first to last

  1. postfix increment and decrement (x++, x--)
  2. prefix increment and decrement (++x, --x)
  3. unary +x, -x, !x
  4. binary arithmetic operators
  5. comparison operators
    (comparison before equality)
  6. logical operators
    (AND before OR)
  7. ternary (conditional) operator
  8. assignment operators

Further reading


Retrieved from http://bmanolov.free.fr/javaoperators.php.